<?php 
/**
 * 添加项目
 * @return string
 */
function addPro(){
	$arr=$_POST;
	$path="./uploads";
	
	$uploadFiles=uploadFile($path);
	$PdoMySQL=new PdoMySQL;
	$PdoMySQL->add($arr,"bac_pro");

	$pid=$PdoMySQL::$lastInsertId;

	if($pid){
		if(is_array($uploadFiles)&&$uploadFiles){
			foreach($uploadFiles as $uploadFile){
				$arr1['pid']=$pid;
				$arr1['albumPath']=$uploadFile['name'];

				addAlbum($arr1);
			}
		}
		$mes="<p>添加成功!</p><a href='addPro.php' target='mainFrame'>继续添加</a>|<a href='listPro.php' target='mainFrame'>查看项目列表</a>";
	}else{

		$mes="<p>添加失败!</p><a href='addPro.php' target='mainFrame'>重新添加</a>";
		
	}
	return $mes;
}
/**
 *编辑项目
 * @param int $id
 * @return string
 */
function editPro($id){
	$arr=$_POST;
	$path="./uploads";

	$uploadFiles=uploadFile($path);


	$PdoMySQL=new PdoMySQL;
	$table="bac_pro";

	$PdoMySQL->update($arr,$table, "id={$id}");


	$pid=$id;
	 if($pid){
		if($uploadFiles &&is_array($uploadFiles)){
			foreach($uploadFiles as $uploadFile){
				$arr1['pid']=$pid;
				$arr1['albumPath']=$uploadFile['name'];
				addAlbum($arr1);
			}
		}
		$mes="<p>编辑成功!</p><a href='listPro.php' target='mainFrame'>查看项目列表</a>";
	}else{
		$mes="<p>编辑失败!</p><a href='listPro.php' target='mainFrame'>重新编辑</a>";
		
	}
	return $mes;
}

function delPro($id){

	$PdoMySQL=new PdoMySQL;
	$table="bac_pro";
	if($PdoMySQL->delete($table,"id=".$id)){
		$proImgs=getAllImgByProId($id);
		if($proImgs&&is_array($proImgs)){
			foreach($proImgs as $proImg){
				if(file_exists("uploads/".$proImg['albumPath'])){
					unlink("uploads/".$proImg['albumPath']);
				}

			}
		}

		$PdoMySQL->delete("bac_album","pid=".$id);
		$mes="删除成功!<br/><a href='listPro.php' target='mainFrame'>查看项目列表</a>";
	}else{
		$mes="删除失败!<br/><a href='listPro.php' target='mainFrame'>重新删除</a>";
	}
	return $mes;
}


/**
 * 得到项目的所有信息
 * @return array
 */
function getAllProByAdmin(){

	$sql = "select p.id, p.pName, p.pArea, p.pDesc, p.cUnit, p.dDesc, p.dTime, c.cName from bac_pro as p join bac_cate c on p.cId = c.id";
	$PdoMySQL=new PdoMySQL;
	return $PdoMySQL->getAll($sql);
}

/**
 *根据项目id得到项目图片
 * @param int $id
 * @return array
 */
function getAllImgByProId($id){

	$PdoMySQL=new PdoMySQL;
	$sql="select a.id, a.albumPath from bac_album a where pid={$id}";
	return $PdoMySQL->getAll($sql);
}

/**
 * 根据id得到项目的详细信息
 * @param int $id
 * @return array
 */
function getProById($id){
	$sql = "select p.id pId, p.pName pName, p.pArea, p.pDesc, p.cUnit, p.dDesc, p.dTime, c.cName, c.id cId from bac_pro as p join bac_cate c on p.cId = c.id where p.id={$id}";

	$PdoMySQL=new PdoMySQL;
	$row=$PdoMySQL->getRow($sql);
	return $row;
}
/**
 * 检查分类下是否有产品
 * @param int $cid
 * @return array
 */
function checkProExist($cid){

	$sql = "select p.id, p.pName, p.pArea, p.pDesc, p.cUnit, p.dDesc, p.dTime from bac_pro p where cId={$cid}";
	$PdoMySQL=new PdoMySQL;
	return $PdoMySQL->getAll($sql);
}

/**
 *得到项目ID和项目名称
 * 
 */
function getProInfo(){

	$PdoMySQL=new PdoMySQL;
	$sql="select id,pName from bac_pro ";
	return $PdoMySQL->getAll($sql);

}

/**
 *项目列表分页查询
 */
function getProByPage($page,$pageSize=2,$whereStr){

	$whereStrs = "p.cid = c.id ".$whereStr;
	$PdoMySQL=new PdoMySQL;
	$tables = "bac_pro p,bac_cate c";
    global $totalRows;
	$totalRows=count($PdoMySQL->find($tables,$whereStrs,'p.id pId, p.pName, p.pArea, p.pDesc, p.cUnit, p.dDesc, p.dTime, c.id, c.cName',null,null,null,null));
	global $totalPage;
	$totalPage=ceil($totalRows/$pageSize);
	if($page<1||$page==null||!is_numeric($page)){
		$page=1;
	}

	if($page>=$totalPage)$page=$totalPage;
	$offset=($page-1)*$pageSize;

	if($offset < 0){
		$offset = 0;
	}

	$rows=$PdoMySQL->find($tables,$whereStrs,'p.id pId, p.pName, p.pArea, p.pDesc, p.cUnit, p.dDesc, p.dTime, c.id, c.cName',null,null,null,array($offset,$pageSize));
	return $rows;

}

/**
 *根据id删除对应图像
 */
function delAlbumById(){
	$ids = $_POST['ids'];
	$PdoMySQL=new PdoMySQL;
	$table = "bac_album";

	$sql="select albumPath from bac_album where id in (".$ids.")";
	$proImgs = $PdoMySQL->getAll($sql);

	if($proImgs&&is_array($proImgs)){
		foreach($proImgs as $proImg){
			if(file_exists("uploads/".$proImg['albumPath'])){
				unlink("uploads/".$proImg['albumPath']);
			}
		}
	}

	if($PdoMySQL->delete($table,"id in(".$ids.")")){
		$mes="删除成功！";
	}else{
		$mes="删除失败！";
	}
	
	echo $mes;

}

